package com.yueya.basic.dict.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.yueya.basic.dict.model.Dict;
import com.yueya.common.action.BaseController;
import com.yueya.common.model.Json;
import com.yueya.common.util.UUIDGenerator;

public class DictController extends BaseController {
	
	@SuppressWarnings("rawtypes")
	Map map = new HashMap();

	public void index() {
		render("/WEB-INF/views/basic/dict/dict_list.jsp");
	}

	@SuppressWarnings("unchecked")
	public void getListPager() {
		map.clear();
		map.put("name", getPara("name"));
		Page<Dict> page = Dict.service.paginate(getPage(), getRows(), map);
		jsonMap.put("total", page.getTotalRow());
		jsonMap.put("rows", page.getList());
		renderJson(JsonKit.toJson(jsonMap));

	}

	public void doAdd() {
		this.render("/WEB-INF/views/basic/dict/dict_edit.jsp");
	}

	public void doEdit() {
		String id = this.getPara("id");
		id = id.replace("/", "");
		Dict dict = Dict.dao.findById(id);
		setAttr("dict", dict);
		this.render("/WEB-INF/views/basic/dict/dict_edit.jsp");
	}

	/**
	 * 保存
	 */
	public void save() {
		Dict dict = this.getModel(Dict.class);
		boolean bl = false;
		if (StringUtils.isNotEmpty(dict.getStr("dict_id"))) {
			bl = dict.update();
		} else {
			dict.set("dict_id", UUIDGenerator.getUUID());
			bl = dict.save();
		}

		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
		} else {
			j.setSuccess(false);
		}
		this.renderJson(j);
	}

	/**
	 * 删除
	 */
	public void delete() {
		String id = this.getPara("id").replace("/", "");
		boolean bl = false;
		bl = Dict.dao.deleteById(id);
		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
		} else {
			j.setSuccess(false);
		}
		this.renderJson(j);
	}

	/**
	 * 批量删除
	 */
	public void deletes() {
		String[] ids = this.getPara("ids").split(";");
		Json j = new Json();
		try {
			for (String id : ids) {
				Dict.dao.deleteById(id);
			}
			j.setSuccess(true);
		} catch (Exception e) {
			j.setSuccess(false);
			e.printStackTrace();
		}
		this.renderJson(j);
	}

	public void getDicValue() {
		String type = this.getPara("type");
		renderJson(JsonKit.toJson(Dict.service.getValues(type)));

	}
	
	public void getValuesOrder() {
		String category = getPara(0);
		renderJson(Dict.service.getValuesOrder(category));
	}

	// 标签
	public void tagList() {
		String type = this.getPara("type");
		renderJson(Dict.service.getJsonValues(type));
	}

}
